package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.packageableElement.valueSpecification;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;

public class OpaqueExpressionItemProvider
		extends ValueSpecificationItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public OpaqueExpressionItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addBodyPropertyDescriptor(object);
			addLanguagePropertyDescriptor(object);
			addResultPropertyDescriptor(object);
			addBehaviorPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	protected void addBodyPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_OpaqueExpression_body_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_OpaqueExpression_body_feature", "_UI_OpaqueExpression_type"),
				UMLPackage.Literals.OPAQUE_EXPRESSION__BODY, true, ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
	}

	protected void addLanguagePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_OpaqueExpression_language_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_OpaqueExpression_language_feature", "_UI_OpaqueExpression_type"),
				UMLPackage.Literals.OPAQUE_EXPRESSION__LANGUAGE, true, ItemPropertyDescriptor.GENERIC_VALUE_IMAGE, null, null));
	}

	protected void addResultPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_OpaqueExpression_result_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_OpaqueExpression_result_feature", "_UI_OpaqueExpression_type"),
				UMLPackage.Literals.OPAQUE_EXPRESSION__RESULT, false, null, null, new String[] { "org.eclipse.ui.views.properties.expert"
				}));
	}

	protected void addBehaviorPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add(createItemPropertyDescriptor(((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory(), getResourceLocator(), getString("_UI_OpaqueExpression_behavior_feature"),
				getString("_UI_PropertyDescriptor_description", "_UI_OpaqueExpression_behavior_feature", "_UI_OpaqueExpression_type"),
				UMLPackage.Literals.OPAQUE_EXPRESSION__BEHAVIOR, true, null, null, null));
	}

	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/OpaqueExpression"));
	}

	@Override
	public String getText(Object object) {
		String label = ((OpaqueExpression) object).getName();
		label = label == null || label.length() == 0 ? getString("_UI_OpaqueExpression_type") :
				getString("_UI_OpaqueExpression_type") + " " + label;

		label = label + " [";
		final List bodies = ((OpaqueExpression) object).getBodies();
		final boolean many = bodies.size() > 1;
		for (final Iterator i = bodies.iterator(); i.hasNext();) {
			final String element = (String) i.next();
			if (many) {
				label = label + "(";
			}
			label = label + element;
			if (many) {
				label = label + ")";
			}
			if (i.hasNext()) {
				label = label + " && ";
			}
		}
		return label + "]";
	}

	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(OpaqueExpression.class)) {
			case UMLPackage.OPAQUE_EXPRESSION__BODY:
			case UMLPackage.OPAQUE_EXPRESSION__LANGUAGE:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
		}
		super.notifyChanged(notification);
	}

	@Override
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);
	}

	@Override
	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

}
